package com.zbkj.crmeb.store.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.utils.DateUtil;
import com.zbkj.crmeb.enums.CrowdCharacteristicsConditionEnum;
import com.zbkj.crmeb.enums.CrowdCharacteristicsEnum;
import com.zbkj.crmeb.store.dao.CrowdCharacteristicsUserDao;
import com.zbkj.crmeb.store.service.CrowdCharacteristicsUserService;
import com.zbkj.crmeb.store.service.StoreUserGroupFeatureService;
import com.zbkj.crmeb.store.service.StoreUserGroupService;
import com.zbkj.crmeb.user.model.User;
import com.zbkj.crmeb.user.service.UserService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 实现类
 *
 * @Author: duyb
 * @Date: 2022/9/28 15:20
 */
@Service
public class CrowdCharacteristicsUserServiceImpl extends ServiceImpl<CrowdCharacteristicsUserDao, User> implements CrowdCharacteristicsUserService {


    @Autowired
    UserService userService;

    @Autowired
    StoreUserGroupService storeUserGroupService;

    @Autowired
    StoreUserGroupFeatureService userGroupFeatureService;

    @Autowired
    CrowdCharacteristicsUserDao crowdCharacteristicsUserDao;

    /**
     * 功能描述: 筛选分群条件中，最近多少天有店铺访问记录的用户
     *
     * @Return: void
     * @Author: duyb
     * @Date: 2022/9/30 15:30
     */
    @Override
    public void SiftAccess(List<User> users, Integer groupId, Integer code, Integer day, Integer merId) {

        //获取最近几天开始的时间
        String conditionTime = DateUtil.getDaysAgo(day);
        //获取最近几天访问的用户
        List<User> userList = this.list(Wrappers.<User>lambdaQuery()
                .eq(User::getMerId, merId)
                .ge(User::getLastLoginTime, conditionTime)
                .groupBy(User::getUid)
        );

        List<Integer> havePurchaseUserIds = userList.stream().map(User::getUid).collect(Collectors.toList());

        Iterator<User> it = users.iterator();

        //当条件为有访问
        if (Objects.equals(CrowdCharacteristicsEnum.HAVE_ACCESS.getCode(), code)) {
            while (it.hasNext()) {
                User user = it.next();
                if (!havePurchaseUserIds.contains(user.getUid())) {
                    it.remove();
                    deleteFeatureId(user, groupId);
                }
            }
        }

        //当条件为无访问
        if (Objects.equals(CrowdCharacteristicsEnum.NO_ACCESS.getCode(), code)) {

            while (it.hasNext()) {
                User user = it.next();
                if (havePurchaseUserIds.contains(user.getUid())) {
                    it.remove();
                    deleteFeatureId(user, groupId);
                }
            }
        }


    }

    /**
     * 功能描述: 判断用户的会员等级
     *
     * @Return: void
     * @Author: duyb
     * @Date: 2022/9/30 11:30
     */
    @Override
    public void MemberLevel(List<User> users, Integer groupId, Integer code, Integer level, Integer merId) {

        //获取符合条件的用户
        List<User> userList = crowdCharacteristicsUserDao.getUserByMemberLevel(merId, level);

        //获取用户id
        List<Integer> havePurchaseUserIds = userList.stream().map(User::getUid).collect(Collectors.toList());

        Iterator<User> it = users.iterator();
        while (it.hasNext()) {
            User user = it.next();
            if (!havePurchaseUserIds.contains(user.getUid())) {
                it.remove();
                deleteFeatureId(user, groupId);
            }
        }
    }

    /**
     * 功能描述: 筛选符合相应条件余额的用户
     *
     * @Return: void
     * @Author: duyb
     * @Date: 2022/9/30 14:22
     */
    @Override
    public void SiftBalance(List<User> users, Integer groupId, Integer code, Integer JudgeCondition, Integer condition_one, Integer condition_two, Integer merId) {

        List<User> userList = null;
        //获取符合条件的用户
        switch (CrowdCharacteristicsConditionEnum.valueOf(JudgeCondition)) {
            case GREATER_THAN:
                userList = this.list(Wrappers.<User>lambdaQuery()
                        .eq(User::getMerId, merId)
                        .ge(User::getNowMoney, condition_one)
                );
                break;
            case LESS_THAN:
                userList = this.list(Wrappers.<User>lambdaQuery()
                        .eq(User::getMerId, merId)
                        .le(User::getNowMoney, condition_one)
                );
                break;
            case SECTION:
                userList = this.list(Wrappers.<User>lambdaQuery()
                        .eq(User::getMerId, merId)
                        .between(User::getNowMoney, condition_one, condition_two)
                );
                break;
        }

        //获取用户id
        List<Integer> havePurchaseUserIds = userList.stream().map(User::getUid).collect(Collectors.toList());

        Iterator<User> it = users.iterator();
        while (it.hasNext()) {
            User user = it.next();
            if (!havePurchaseUserIds.contains(user.getUid())) {
                it.remove();
                //将user中对应的featureId删除
                deleteFeatureId(user, groupId);
            }
        }
    }

    /**
     * 功能描述: 判断用户是否为付费会员
     *
     * @Return: void
     * @Author: duyb
     * @Date: 2022/9/30 15:22
     */
    @Override
    public void WhetherPaidMember(List<User> users, Integer groupId, Integer code, Integer JudgeCondition, Integer condition_one, Integer merId) {

        //获取符合条件的用户
        List<User> userList = crowdCharacteristicsUserDao.getUserByPaidMember(merId);

        //获取用户id
        List<Integer> havePurchaseUserIds = userList.stream().map(User::getUid).collect(Collectors.toList());

        Iterator<User> it = users.iterator();
        //1:付费会员,0:非付费会员
        if (condition_one == 1) {
            while (it.hasNext()) {
                User user = it.next();
                if (!havePurchaseUserIds.contains(user.getUid())) {
                    it.remove();
                    //将user中对应的featureId删除
                    deleteFeatureId(user, groupId);
                }
            }
        }
        if (condition_one == 0) {
            while (it.hasNext()) {
                User user = it.next();
                if (havePurchaseUserIds.contains(user.getUid())) {
                    it.remove();
                    //将user中对应的featureId删除
                    deleteFeatureId(user, groupId);
                }
            }
        }

    }

    /**
     * 功能描述: 删除用户群组id
     *
     * @Param: [user, groupId]
     * @Return: void
     * @Author: duyb
     * @Date: 2022/10/08 11:51
     */
    public void deleteFeatureId(User user, Integer groupId) {
        //groupId为0时，仅计算人数，不删除user中对应的groupId
        if (groupId == 0){
            return;
        }
        //将JsonArray数组转化为List
        if (StringUtil.isNotBlank(user.getFeatureId())){
            List<String> featureIds = new ArrayList<>(Arrays.asList(user.getFeatureId().split(",")));

            if (featureIds.size()>0 && featureIds.contains(String.valueOf(groupId))) {
                featureIds.remove(String.valueOf(groupId));
                userService.update(new UpdateWrapper<User>().lambda()
                        .eq(User::getUid, user.getUid())
                        .set(User::getFeatureId, featureIds.stream().collect(Collectors.joining(","))));
            }
        }

    }



}
